package com.monika.main.security.service;

import com.monika.main.system.domain.entity.LoginUser;
import com.monika.main.system.domain.entity.SysUser;
import com.monika.main.system.servcie.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Author whh
 * Date 2023/04/17/ 20:45
 * <p></p>
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = sysUserService.querySysUserCache(username, 10, TimeUnit.MINUTES);
        if(Objects.isNull(user)){
            return null;
        }
        return new LoginUser(user.getUser_id(), user.getDept_id(), user, Collections.EMPTY_SET);
    }
}
